package cn.jas.common.jascommon.util;


import cn.jas.common.jascommon.entity.DateLabel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description: 日期处理类
 * @author: chen_mx
 **/
public class DateUtil {
    static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<>();

    public static String formateString(Date date, String formatter) {
        local.set(new SimpleDateFormat(formatter));
        return local.get().format(date);
    }

    public static Date parseString(String date) throws ParseException {
        local.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return local.get().parse(date);
    }

    public static Date parseString(String date, String formatter) throws ParseException {
        local.set(new SimpleDateFormat(formatter));
        return local.get().parse(date);
    }

    /**
     * 算出截至天数
     */
    public static String calculateClosingTime(Date date) {
        Calendar cal = Calendar.getInstance();
        Long now = cal.getTimeInMillis();
        cal.setTime(date);
        Long times = cal.getTimeInMillis();
        if (times < now) {
            return "0";
        }
        long day = (times - now) / (60 * 60 * 24 * 1000);
        return String.valueOf((int) Math.ceil(day));
    }


    /**
     * 获取本季度截至时间
     * 获取本季度最后一天时间
     */
    public static String getQuarterTime() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        if (month < 4) {
            return cal.get(Calendar.YEAR) + "-3-31 23:59:00";
        }
        if (month < 7) {
            return cal.get(Calendar.YEAR) + "-6-30 23:59:00";
        }
        if (month < 10) {
            return cal.get(Calendar.YEAR) + "-9-30 23:59:00";
        }
        if (month > 9) {
            return cal.get(Calendar.YEAR) + "-12-31 23:59:00";
        }
        return "";
    }

    /**
     * 获取当前时间的 一个季度的时间
     */
    public static String getQuarterMiniTime() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        cal.add(Calendar.MONTH, 4); //month  值少1 所以一个季度需要加上4
        String resMonth = cal.get(Calendar.MONTH) < 10 ? "0" + cal.get(Calendar.MONTH) : cal.get(Calendar.MONTH) + "";
        String resDay = cal.get(Calendar.DAY_OF_MONTH) < 10 ? "0" + cal.get(Calendar.DAY_OF_MONTH) : cal.get(Calendar.DAY_OF_MONTH) + "";
        String resHour = cal.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + cal.get(Calendar.HOUR_OF_DAY) : cal.get(Calendar.HOUR_OF_DAY) + "";
        String resMinute = cal.get(Calendar.MINUTE) < 10 ? "0" + cal.get(Calendar.MINUTE) : cal.get(Calendar.MINUTE) + "";
        return cal.get(Calendar.YEAR) + "-" + resMonth + "-" + resDay + " " + resHour + ":" + resMinute;
    }

    /**
     * 获取一周的 日期标签
     */
    @SuppressWarnings("all")
    public static List<DateLabel> getWeekDayList() {
        Calendar cal = Calendar.getInstance();
        List<DateLabel> list = new ArrayList<>();
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w <= 0) {
            w = 0;
        } else {
            cal.add(Calendar.DAY_OF_WEEK, -w);
            for (String str : weekDays) {
                int corrent = cal.get(Calendar.DAY_OF_WEEK) - 1;
                DateLabel dl = new DateLabel();
                dl.setWeekday(weekDays[corrent]);
                dl.setDatetime(formateString(cal.getTime(), "yyyy-MM-dd"));
                list.add(dl);
                cal.add(Calendar.DAY_OF_WEEK, 1);
            }
        }
        return list;
    }

    /**
     * 获取指定一周的日志 标签
     **/
    @SuppressWarnings("all")
    public static List<DateLabel> getWeekDayList(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        List<DateLabel> list = new ArrayList<>();
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w <= 0) {
            w = 0;
        } else {
            cal.add(Calendar.DAY_OF_WEEK, -w);
            for (String str : weekDays) {
                int corrent = cal.get(Calendar.DAY_OF_WEEK) - 1;
                DateLabel dl = new DateLabel();
                dl.setWeekday(weekDays[corrent]);
                dl.setDatetime(formateString(cal.getTime(), "yyyy-MM-dd"));
                list.add(dl);
                cal.add(Calendar.DAY_OF_WEEK, 1);
            }
        }
        return list;
    }


    public static void main(String[] args) {

        //System.out.println(getQuarterMiniTime());
        try {
            System.out.println(getWeekDayList(parseString("2019-07-23", "yyyy-MM-dd")));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}
